using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MobileMake.Models;
using MobileMake.Models.Validation;

namespace MobileMake.Controllers
{

    public class PortalController : Controller
    {
        private IDataService _service;

        #region Constructs

        public PortalController()
        {
            _service = new DataService(new ModelStateWrapper(this.ModelState));
        }

        public PortalController(IDataService service)
        {
            _service = service;
        }

        #endregion

        //
        // GET: /Portal/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult IndexData(int page, int rows, string search, string sidx, string sord)
        {
            var model = from entity in _service.GetPortalsQueryable().OrderBy(sidx + " " + sord) 
                        select new 
                        {
                            PortalID = entity.PortalID, 
                            PortalName = entity.PortalName,
                            Description = entity.Description,
                            KeyWords = entity.KeyWords 
                        };

            return Json(model.ToJqGridData(page, rows, null, search,
                new[] { "PortalID", "PortalName", "Description", "KeyWords" }));
        }

        //
        // POST: /Portal/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {       
            Portal portalToAdd = new Portal();

            TryUpdateModel<Portal>(portalToAdd, new[] { "PortalName", "Description", "KeyWords" }, collection.ToValueProvider());
            
            if (!_service.AddPortal(portalToAdd))
            {
                return Json(new OperationResultInfo
                {
                    Successfull = false,
                    Errors = ModelState.GetAllErrors()
                });
            }

            return Json(new OperationResultInfo { Successfull = true, });
        }

        //
        // POST: /Portal/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int? id, FormCollection collection)
        {         
            Portal portalToUpdate = _service.GetPortal(id);

            TryUpdateModel<Portal>(portalToUpdate, new[] { "PortalName", "Description", "KeyWords" }, collection.ToValueProvider());
            
            if (!_service.UpdatePortal(portalToUpdate))
            {
                return Json(new OperationResultInfo
                {
                    Successfull = false,                        
                    Errors = ModelState.GetAllErrors()
                });
            }
            
            return Json(new OperationResultInfo { Successfull = true, });
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id)
        {
            Portal portalToDelete = _service.GetPortal(id);
            
            if (!_service.DeletePortal(portalToDelete))
            {
                return Json(new OperationResultInfo
                {
                    Successfull = false,
                    Errors = ModelState.GetAllErrors()
                });
            }

            return Json(new OperationResultInfo{Successfull = true,});

        }
    }
}
